package
{
	import com.greensock.TweenLite;
	
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.events.TimerEvent;
	import flash.utils.Timer;

	public class FWBSimulator
	{
		public var display:Sprite = new Sprite();
		private var fwbSlots:Vector.<FWBSlot> = new Vector.<FWBSlot>;
		private var m_simulationActionTimer:Timer = new Timer(1000);
	
		public function FWBSimulator()
		{
			m_simulationActionTimer.addEventListener(TimerEvent.TIMER, onSimulationTick);
			m_simulationActionTimer.start();
			var fwb:FWBSlot
			 fwbSlots.push(new FWBSlot(new SNUser('Brian', 'assets/profile_pics/brian_r.jpg'), Math.random()*100000));
			 fwbSlots.push(new FWBSlot(new SNUser('Helen', 'assets/profile_pics/helen_z.jpg'), Math.random()*100000));
			 fwbSlots.push(new FWBSlot(new SNUser('Jesse', 'assets/profile_pics/jesse_smith.jpg'), Math.random()*100000));
			 fwbSlots.push(new FWBSlot(new SNUser('Mark', 'assets/profile_pics/mark_n.jpg'), Math.random()*100000));
			 fwbSlots.push(new FWBSlot(new SNUser('Paul', 'assets/profile_pics/paul_s.jpg'), Math.random()*100000));
			 fwbSlots.push(new FWBSlot(new SNUser('Vinh', 'assets/profile_pics/vinh_t.jpg'), Math.random()*100000));
			 var i:int, length:int;
			 for(i = 0, length = fwbSlots.length; i < length; i++){
				 display.addChild(fwbSlots[i].display);
			 }
			 
			 updateFWBPositions();
		}
		
		private static var m_bonusConfig:Array = [];
		
		private static function getBonus():Object{
			if(!m_bonusConfig.length){
				setupBonuses();
			}
			
			var bonusWeights:Array = [1,4,8,16];
			var totalWeight:int;
			for each(var weight:int in bonusWeights){
				totalWeight += weight;
			}
			var random:int = Math.floor(Math.random()*totalWeight);
			var index:int = 0;
			var curWeight:int = 0;
			for each(weight in bonusWeights){
				curWeight += weight;
				if(random < curWeight){
					break;
				} else {
					index++;
				}
			}

			return m_bonusConfig[index];
		}
		
		private static function setupBonuses():void{
			var bonus:Object;

			bonus = new Object();
			bonus['level'] = 4;
			bonus['message'] = 'ULTRA COMBO BONUS!!!';
			bonus['scoreBase'] = 100000;
			m_bonusConfig.push(bonus);
			
			bonus = new Object();
			bonus['level'] = 3;
			bonus['message'] = 'Triple Combo bonus!!';
			bonus['scoreBase'] = 50000;
			m_bonusConfig.push(bonus);
			
			bonus = new Object();
			bonus['level'] = 2;
			bonus['message'] = 'Double Combo Bonus!';
			bonus['scoreBase'] = 25000;
			m_bonusConfig.push(bonus);
			
			bonus = new Object();
			bonus['level'] = 1;
			bonus['message'] = 'Combo Bonus';
			bonus['scoreBase'] = 10000;
			m_bonusConfig.push(bonus);	
		}
		

		
		private function onSimulationTick(te:TimerEvent):void{
			var hit:int = Math.floor(Math.random()*3);
			if(hit == 1){
				var fwbSlot:FWBSlot = fwbSlots[Math.floor(Math.random()*fwbSlots.length)];
				var bonus:Object = getBonus();
				var fwbScore:Number = fwbSlot.score;
				fwbSlot.udpateScore(fwbScore + bonus['scoreBase']*(1 + Math.random())/2);
				fwbSlot.updateMessageField(bonus['message']);
				var item:GameObject = new GameObject();
				item.type = GameObject.getRandomType();
				item.level = bonus['level'];
				item.updateDisplay();
				
				fwbSlot.bonusItem = item;
				fwbSlot.applyBonusItem(item.display);
				updateFWBPositions();
			}
		}
		
		private function fwbSort(a:FWBSlot, b:FWBSlot):Number{
			return b.score - a.score;
		}
		
		public function updateFWBPositions():void{
			var sortedFWBSlots:Vector.<FWBSlot> = fwbSlots.sort(fwbSort);
			var i:int, length:int;
			for(i = 0, length = sortedFWBSlots.length; i < length; i++){
				var fwbDisplay:Sprite = sortedFWBSlots[i].display;
				TweenLite.to(fwbDisplay, 1, {y: FWBSlot.HEIGHT*i + i});
			}
		}
	}
}